package com.huluspace.chat.controller;

import com.huluspace.chat.function.WeatherNow;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.model.function.FunctionCallback;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/chat")
public class ChatController {
  private final ChatClient chatClient;
  private final WeatherNow weatherNow;

  public ChatController(ChatClient.Builder builder,
                        WeatherNow weatherNow) {
    this.weatherNow = weatherNow;
    this.chatClient = builder.build();
  }

  @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
  public Flux<String> stream(@RequestParam(name = "message", required = false) String message) {
    return chatClient.prompt()
        .user(message)
        .stream()
        .content();
  }

  @GetMapping("/weather")
  public String getWeatherWithAI(@RequestParam(name = "query", required = false) String query) {
    return chatClient.prompt()
        .user(query)
        .tools(weatherNow)
        .call()
        .content();
  }
}
